/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.listener;

import java.net.*;

import javax.net.*;

import java.io.*;

import nfse.NetFSE;

public class NetListenerServer extends Thread {
    private int port = -1;
    private int partitionSize = 100;

    public NetListenerServer(int portNum) {
        this.port = portNum;
    }
    
    public NetListenerServer(int portNum, int partitionSize) {
        this.port = portNum;
        this.partitionSize = partitionSize;
    }

    public int getPort() {
        return port;
    }
    
    public int getPartitionSize() {
        return partitionSize;
    }
    
    public static void main(String[] args) {
        try {
            System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");

            if (args.length < 2) {
                System.out.println("Usage: java -jar nfse.jar config_file node_config_file");
                return;
            }
            
            NetFSE.CONFIG_FILE = args[args.length - 2];
            String configPath = NetFSE.CONFIG_FILE;
            File config = new File(configPath);
            if (config.exists())
               NetFSE.configure(configPath);
            else {
                throw (new Exception("The specified configuration file does not exist: '" + configPath + "'"));
            }
            
            String nodeConfigPath = args[args.length - 1];
            File config2 = new File(nodeConfigPath);
            if (config2.exists()) {
                NetFSE.processNodeConfigFile(config2);
            } else {
                throw (new Exception("The specified node configuration file does not exist: '" + nodeConfigPath + "'"));
            }

              
            NetListenerServer nss = new NetListenerServer(NetFSE.NET_SENSOR_SERVER_PORT);
            nss.start();
            System.out.println("Started Net/Listener");

           
        } catch (Exception E) {
            E.printStackTrace();
        }
    }

    public void run() {
        try {
        	
            System.out.println("Net/Listener: Listening on port " + port + "");
            ServerSocketFactory ssf = null;
            //ssf = SSLServerSocketFactory.getDefault();
            ssf = ServerSocketFactory.getDefault();
            ServerSocket ss = ssf.createServerSocket(port);
            
            NetListenerSocketMonitor dsm = new NetListenerSocketMonitor(300);
			dsm.start();
			
            while (true) {
                NetListenerSocket sock = new NetListenerSocket(ss.accept(), this, true);
                dsm.addNetListenerSocket(sock);
                sock.start();
                System.out.println("Net/Listener: Opened new Net/Listener connection");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Stopping Net/Listener");
        }
    }

}